package sales.gz.com.salescounter.utils;

/**
 * Created by gzjd on 2017/5/25.
 */

import android.serialport.JNI.SerialPort;
import android.util.Log;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class N01_Dev_YM {
    private SerialPort serialPort;
    private OutputStream portOutStream;
    private InputStream portInStream;
    private final int waitDly = 200;
    public enum DevStatus{IDLE,BUSY,TIMEOUT,NORMAL,KEEPOUT};
    private  int DevCount=0;
    public N01_Dev_YM(String port){
        try {
            serialPort = new SerialPort(new File(port), 9600, 8, 1, "N");
            if (serialPort != null){
                portOutStream = serialPort.getOutputStream();
                portInStream = serialPort.getInputStream();
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    private static String byte2hex(byte [] buffer){
        String h = "";

        for(int i = 0; i < buffer.length; i++){
            String temp = Integer.toHexString(buffer[i] & 0xFF);
            if(temp.length() == 1){
                temp = "0" + temp;
            }
            h = h + " "+ temp;
        }

        return h;

    }

    //开始出货 row列，line行
    public DevStatus Start(int row, int line){
        Log.i("log", "send ok");
        byte[] cmd = {(byte)0xAA, 0x53, 0x00,0x00,0x00,0x00,0x00,0x00,(byte)0xAC};
        try {
            cmd[2] = (byte)(row+0x30);
            cmd[3] = (byte)(line+0x30);
            byte check=0;
            for (int i=1;i<cmd.length-2;i++) {
                check ^= cmd[i];
            }
            cmd[cmd.length-2] = check;
            byte[] buffer = new byte[64];
            //清空接收缓存
            int size  = portInStream.read(buffer,0,buffer.length);
            DevStatus DevStatu=Find_Status(row,line);
            if(DevStatu==DevStatus.IDLE) {
                //发送命令
                portOutStream.write(cmd);
            }
            //接收返回数据
            Thread.sleep(waitDly);
            size = this.portInStream.read(buffer, 0, buffer.length);
            Log.i("chuhuo",byte2hex(buffer));
            Log.i("sizede",size+"----------------"+buffer.length);
            // Find_Status(row,line);
            //Send_HeartBeat(row,line);
            if (size > 3) {
                Log.i("CCCC",size+"");
                if (buffer[1] == 0x53 && buffer[2] == 0x30) {
                    Log.i("CCCC","SUCSESS");
                    return DevStatus.IDLE;
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return DevStatus.TIMEOUT;
    }
    //查询货道状态 row列，line行
    DevStatus Find_Status(int row,int line){
        byte[] cmd = {(byte)0xAA, 0x52, 0x00,0x00,0x00,0x00,0x00,0x00,(byte)0xAC};
        try {
            cmd[2] = (byte)(row+0x30);
            cmd[3] = (byte)(line+0x30);
            byte check=0;
            for (int i=1;i<cmd.length-2;i++) {
                check ^= cmd[i];
            }
            cmd[cmd.length-2] = check;
            byte[] buffer = new byte[64];
            //清空接收缓存
            int Stasize  = portInStream.read(buffer,0,buffer.length);
            //发送命令
            portOutStream.write(cmd);
            //接收返回数据
            Thread.sleep(waitDly);
            Stasize = this.portInStream.read(buffer, 0, buffer.length);
            Log.i("chaxun",byte2hex(buffer));
            Log.i("size=",Stasize+"====");
            if (Stasize > 3) {
                if (buffer[1] == 0x52) {
                    switch (buffer[2]) {
                        case 0x30:
                            return DevStatus.BUSY;
                        case 0x31:
                            return DevStatus.IDLE;
                        case 0x32:
                            return DevStatus.TIMEOUT;
                        case 0x34:
                            return DevStatus.KEEPOUT;
                        default:
                            break;
                    }
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return DevStatus.TIMEOUT;
    }
    //发送心跳包
    DevStatus Send_HeartBeat(int row,int line){
        byte[] cmd = {(byte)0xAA, 0x48, 0x00,0x00,0x00,0x00,0x00,0x00,(byte)0xAC};
        try {
            cmd[2] = (byte)(row+0x30);
            cmd[3] = (byte)(line+0x30);
            byte check=0;
            for (int i=1;i<cmd.length-2;i++) {
                check ^= cmd[i];
            }
            cmd[cmd.length-2] = check;
            byte[] buffer = new byte[64];
            //清空接收缓存
            int size  = portInStream.read(buffer,0,buffer.length);
            //发送命令
            portOutStream.write(cmd);
            //接收返回数据
            Thread.sleep(waitDly);
            size = this.portInStream.read(buffer, 0, buffer.length);
            Log.i("xintiao",byte2hex(buffer));
            if (size > 3) {
                if (buffer[1] == 0x48 && buffer[2] == 0x48) {
                    return DevStatus.IDLE;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return DevStatus.TIMEOUT;
    }
    public  void PutStream(List<String> data)
    {
        while(data.size()>0)
        {
            if(DevCount < 10000)
            {
                DevCount++;
                //  Log.i("zcont","count="+DevCount);
            }else {
                int row = Integer.parseInt(data.get(0).substring(0, 1));
                int line = Integer.parseInt(data.get(0).substring(1, 2));
                DevStatus devStatu = Find_Status(row, line);
                if (devStatu == DevStatus.IDLE) {
                    Start(row, line);
                    Log.i("zc", data.get(0) + "  row=" + row + "  line=" + line);
                    data.remove(0);
                }
                DevCount=0;
            }
        }
//        }
    }
}
